//
//  CCSave.m
//  HaiYue
//
//  Created by skyline on 2018/1/22.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "CCSave.h"

@implementation CCSave
+ (NSString *)getDocumentsPath
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
}

+ (NSString *)getCachesPath
{
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
}

+ (NSString *)getPreferencesPath
{
    return [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"Preferences"];
}

+ (NSString *)getTmpPath
{
    return NSTemporaryDirectory();
}

//NSUserDefaults 存储
+ (void)value:(id)data forKey:(NSString *)key
{
    if (!data || !key)  return;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];//NSUserDefaults数据保存在沙盒路径Preferences下
    [defaults setObject:data forKey:key];
    [defaults synchronize];
}

//NSUserDefaults 读取
+ (id)valueFromKey:(NSString*)key
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}

//NSUserDefaults 删除
+ (void)rmValueWithKey:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}

//存储到caches路径
+ (BOOL)plist:(id)value toFile:(NSString*)file
{
    if (!value || !file)
        return NO;
    NSString *new_plist = ([file containsString:@".plist"])?[NSString stringWithFormat:@"/%@",file]:[NSString stringWithFormat:@"/%@.plist",file];
    NSString *path = [[CCSave getCachesPath] stringByAppendingString:new_plist];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        /**
         1.[value writeToFile:path atomically:YES];
         2.[NSKeyedArchiver archiveRootObject:value toFile:path]
         3.使用NSKeyedArchiver可以归档NSString、NSArray、NSDictionary、NSSet、NSDate、NSNumber和NSData等基本的Foundation对象
         */
        [NSKeyedArchiver archiveRootObject:value toFile:path];
    });
    return YES;
}

//从caches路径读取数据
+ (id)plistFromFile:(NSString *)file
{
    if (!file)
    {
        return nil;
    }
    NSString *new_plist = ([file containsString:@".plist"])?[NSString stringWithFormat:@"/%@",file]:[NSString stringWithFormat:@"/%@.plist",file];
    NSString *path = [[CCSave getCachesPath] stringByAppendingString:new_plist];
    /**
     1.从plist读取，要得到NSArray、NSDictionary的类型，使用[NSArray arrayWithContentsOfFile:path] 或者[NSDictionary dictionaryWithContentsOfFile:path]不同的方法
     2.使用NSKeyedUnarchiver可以恢复NSString、NSArray、NSDictionary、NSSet、NSDate、NSNumber和NSData等基本的Foundation对象
     */
    return [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}


//@pragma 归档
+ (void)archiver:(id)object toFile:(NSString *)file
{
    NSString * new_file = ([file containsString:@".plist"])?[NSString stringWithFormat:@"/%@",file]:[NSString stringWithFormat:@"/%@.plist",file];
    //    NSString * new_file = ([file containsString:@".archiver"])?[NSString stringWithFormat:@"/%@",file]:[NSString stringWithFormat:@"/%@.archiver",file];
    NSString *path = [[CCSave getCachesPath] stringByAppendingString:new_file];
    [NSKeyedArchiver archiveRootObject:object toFile:path];
}
//@pragma 解档
+ (id)unarchiverObjFromFile:(NSString *)file
{
    NSString * new_file = ([file containsString:@".plist"])?[NSString stringWithFormat:@"/%@",file]:[NSString stringWithFormat:@"/%@.plist",file];
    //    NSString * new_file = ([file containsString:@".archiver"])?[NSString stringWithFormat:@"/%@",file]:[NSString stringWithFormat:@"/%@.archiver",file];
    NSString *path = [[CCSave getCachesPath] stringByAppendingString:new_file];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}



@end
